/**
 *    @file       listenthread.cpp
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/19/2014 03:53:31 PM
 *
 *    @author     Theophilus (), wangtf418@gmail.com 
 */
#include "listenthread.h"
#include "server.h"

atomic_uint32 listenthread::m_initflag;

listenthread::listenthread(bf_uint32_t type, bf_uint32_t size, bf_uint32_t index, const comargs *args)
	: netprocessthread(type, size, index, args)
{

}

listenthread::~listenthread()
{

}

bf_int_t listenthread::init()
{
	if (m_initflag.add_return(1) > 1)
	{
		return BF_ERROR;
	}

	srv_addr_args *args = (srv_addr_args*)m_args;
	if (!args)
	{
		return BF_ERROR;
	}
	
	bf_int_t ret = netprocessthread::init();
	if (ret != BF_OK)
	{
		return BF_ERROR;
	}

	csocket *sock = new csocket(args->m_srvip, args->m_srvport);

	sockettask *task = server::get()->get_classfactory()->gen_sockettask(TSK_TYPE_TCP_LISTEN, sock, this, m_args);
	if (!task)
	{
		return BF_ERROR;
	}

	ret = task->init(0);
	if (ret != BF_OK)
	{
		delete task;
		return BF_ERROR;
	}

	return BF_OK;
}

